package com.xinxing.learning.batch.increment.config;

import com.xinxing.learning.batch.common.SyncConstants;
import com.xinxing.learning.batch.entity.User;
import com.xinxing.learning.batch.increment.listener.IncrementJobEndListener;
import com.xinxing.learning.batch.increment.step.IncrementUserItemProcessor;
import com.xinxing.learning.batch.increment.step.IncrementUserItemReader;
import com.xinxing.learning.batch.increment.step.IncrementUserItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
@EnableBatchProcessing
public class IncrementBatchConfig {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    @StepScope
    public ItemReader incrementReader(@Value("#{jobParameters['lastUpdateTime']}") String lastUpdateTime) {
        HashMap<String, Object> params = new HashMap<>();
        params.put(SyncConstants.STR_LAST_UPDATE_TIME, lastUpdateTime);
        IncrementUserItemReader incrementUserItemReader = new IncrementUserItemReader();
        incrementUserItemReader.setParams(params);
        return incrementUserItemReader;
    }

    @Bean
    public ItemProcessor incrementProcessor() {
        return new IncrementUserItemProcessor();
    }

    @Bean
    public ItemWriter incrementWriter() {
        return new IncrementUserItemWriter();
    }

    @Bean
    public JobExecutionListener incrementListener() {
        return new IncrementJobEndListener();
    }

    @Bean
    public Step incrementStep(ItemReader itemReader, ItemProcessor itemProcessor, ItemWriter itemWriter) {
        String funcName = Thread.currentThread().getStackTrace()[0].getMethodName();
        return stepBuilderFactory.get(funcName)
                .<User, User>chunk(2)
                .reader(itemReader)
                .processor(itemProcessor)
                .writer(itemWriter)
                .build();
    }

    @Bean
    public Job incrementJob(Step incrementStep, JobExecutionListener incrementListener) {
        String funcName = Thread.currentThread().getStackTrace()[0].getMethodName();
        return jobBuilderFactory.get(funcName)
                .listener(incrementListener)
                .flow(incrementStep)
                .end().build();

    }
}
